/**
 * Created by qingteng.wang on 2017/1/9.
 */

+function ($) {
    $(function () {
        const $khNwScroll = $('#khNwScroll');
        const $khNwList = $khNwScroll.find('.kh-nw-list');
        const $khNwItem = $khNwScroll.find('.kh-nw-item');
        const defaultHeight = $khNwItem.eq(0).innerHeight();
        const size = $khNwItem.size();
        let curHeight;
        let count = 0;
        let totalHeight = 0;
        let timerId = null;
        let hoverFlag = false;
        curHeight = defaultHeight;
        totalHeight = curHeight;


        $khNwScroll.height(defaultHeight * 5);

        $khNwList.clone().appendTo($khNwScroll);


        $khNwScroll.hover(function () {
            hoverFlag = true;
            timerId && clearInterval(timerId);
        }, function () {
            hoverFlag = false;
            timerId = setInterval(scorll, 2000);
        });


        timerId = setInterval(scorll, 2000);

        function scorll() {
            let $khnwlist = $khNwScroll.find('.kh-nw-list').eq(0);
            $khnwlist.animate({
                'marginTop': -totalHeight
            }, 400, 'linear', function () {
                if (hoverFlag) {
                    return
                }
                if (count == size) {
                    count = 0;
                    $khnwlist.css('marginTop', 0);
                    totalHeight = curHeight;
                }
            });
            totalHeight += $khnwlist.find('.kh-nw-item').eq(count).innerHeight();
            count++;
        }

    })
}(jQuery);